package project.innovation.mooddiagnosis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import project.innovation.mooddiagnosis.dao.UserMapper;
import project.innovation.mooddiagnosis.entity.result.Result;
import project.innovation.mooddiagnosis.entity.user.UserInfo;
import project.innovation.mooddiagnosis.service.UserService;

import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 得到用户的openId
     * @param map request body 需要code
     * @return Result 用户的openId
     * @throws IOException
     */
    @PostMapping("/openId")
    public Result getOpenId(@RequestBody Map<String, String> map) throws IOException {
        String code = map.get("code");
        String openId = null;
        openId = userService.getOpenIdFromWX(code);

        userMapper.insertUser(openId, "");

        return new Result(200, "openId获得成功", openId);
    }
}
